package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.Carousel;
import service.CarouselService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "*")
@RequestMapping("/carousel")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;

    @GetMapping("/findAll")
    @ResponseBody
    public List<Carousel> findAll() {
        return carouselService.findAll();
    }

    @GetMapping("/findById")
    @ResponseBody
    public Carousel findById(@RequestParam Integer id) {
        return carouselService.findById(id);
    }

    @PostMapping("/add")
    @ResponseBody
    public Map<String, Object> add(@RequestBody Carousel carousel) {
        Map<String, Object> result = new HashMap<>();
        int count = carouselService.add(carousel);
        result.put("success", count > 0);
        result.put("message", count > 0? "添加成功" : "添加失败");
        return result;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Carousel carousel) {
        Map<String, Object> result = new HashMap<>();
        int count = carouselService.update(carousel);
        result.put("success", count > 0);
        result.put("message", count > 0? "更新成功" : "更新失败");
        return result;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam Integer id) {
        Map<String, Object> result = new HashMap<>();
        int count = carouselService.delete(id);
        result.put("success", count > 0);
        result.put("message", count > 0? "删除成功" : "删除失败");
        return result;
    }
}